from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from utils.driver import browser_driver


class PageEcshopBackPreferential():
    ''' 后台Preferential activitie优惠活动 '''

    def __init__(self, driver):
        driver = browser_driver()
        self.driver = driver
        # 元素定位器
        self.loc_preferential_add = (By.LINK_TEXT, "添加优惠活动")  # 添加优惠活动按钮
        self.loc_preferential_act_name = (By.ID, "act_name")  # 优惠活动名称
        self.loc_preferential_act_type = (By.ID, "act_type")  # 优惠方式选择
        self.loc_preferential_act_type_ext = (By.ID, "act_type_ext")  # 优惠活动名称
        self.loc_preferential_act_submit = (By.XPATH, "//input[@value='确定']")  # 提交按钮

    def preferential_add(self):
        self.driver.find_element(*self.loc_preferential_add).click()

    def preferential_act_name(self, name):
        self.driver.find_element(*self.loc_preferential_add).clear()
        self.driver.find_element(*self.loc_preferential_add).send_keys(name)

    def preferential_select(self):
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[1]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[2]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[3]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[4]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[5]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[6]').click()
        self.driver.find_element(By.XPATH, '/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[7]').click()

    def preferential_act_type(self):
        element = self.driver.find_element(*self.loc_preferential_act_type)
        Select(element).select_by_visible_text("享受现金减免")

    def preferential_act_type_ext(self, money):
        self.driver.find_element(*self.loc_preferential_act_type_ext).clear()
        self.driver.find_element(*self.loc_preferential_act_type_ext).send_keys(money)
